import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:psi/base/base_controller.dart';
import 'package:psi/entity/common_resp.dart';
import 'package:psi/entity/response/login_resp.dart';
import 'package:psi/services/user_logic.dart';
import 'package:psi/utils/utils.dart';

import '../../../http/api_service.dart';
import 'me_state.dart';

class MeLogic extends BaseController {
  final MeState state = MeState();

  @override
  void onReady() async {
    // TODO: implement onReady
    super.onReady();
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String appName = packageInfo.appName;
    String packageName = packageInfo.packageName;
    String version = packageInfo.version;
    String buildNumber = packageInfo.buildNumber;
    state.menus[1].subTitle = version;
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  void logout() async {
    // 接口退出登录
    await ApiService.request.logout();
    Utils.userLogic.logout();
  }

  void changeAvatar() async {
    String? imageUrl = await Utils.selectImage();
    if (imageUrl != null) {
      User user = Utils.userLogic.user.value;
      user.avatar = imageUrl;
      try {
        CommonResp resp = await ApiService.request.updateUserInfo(user);
        if (resp.code == 0) {
          Utils.userLogic.user.value = user;
          Utils.userLogic.update(["userInfo"]);
        } else {
          showToast(resp.message);
        }
      } catch(e) {
        Utils.logE(e);
      }
    }


  }
}
